package test.java;/**
 * Created by hunter.coder 涛哥
 * 2019/3/29 11:36
 * 交流qq:657270652
 * Version: 1.0
 * 更多学习资料：https://blog.csdn.net/coderblack/
 * Description:
 **/

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;

import java.io.IOException;
import java.util.ArrayList;

/**
 * @Author hunter.coder
 * @Date 2019/3/29 11:36
 * @Version 1.0
 * @Description TODO
 **/
public class HttpClientGaode {

    public static void main(String[] args) throws IOException {
        // 构造一个http客户端对象
        HttpClient httpClient = new HttpClient();

        // 构造一个get请求对象
        GetMethod get = new GetMethod("https://restapi.amap.com/v3/geocode/regeo?key=41e7ce0e00fc0d5eaeaccb4cc1f30297&location=116.481488,39.990464");

        // 用客户端去执行构造好的get请求，请求的结果会直接封装到get对象中
        httpClient.executeMethod(get);

        //从get对象中获取响应
        String resJson = get.getResponseBodyAsString();

        // 获取省市区及商圈信息
        JSONObject jsonObject = JSON.parseObject(resJson);

        JSONObject regeocode = jsonObject.getJSONObject("regeocode");
        JSONObject addressComponent = regeocode.getJSONObject("addressComponent");

        String province = addressComponent.getString("province");
        String city = addressComponent.getString("city");
        String district = addressComponent.getString("district");

        JSONArray businessAreas = addressComponent.getJSONArray("businessAreas");

        ArrayList<JSONObject> bizList = new ArrayList<>();

        for(int i=0;i<businessAreas.size();i++){
            JSONObject biz = businessAreas.getJSONObject(i);
            String location = biz.getString("location");
            String name = biz.getString("name");
            bizList.add(biz);
        }


        System.out.println(province + " " + city + " " + district);
        System.out.println(bizList);



    }

}
